package com.renli.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.renli.exception.RenLiException;
import com.renli.model.domain.WorkDay;
import com.renli.model.dto.UpdateDayDto;
import com.renli.result.R;
import com.renli.service.PunishmentService;
import com.renli.service.StaffWagesService;
import com.renli.service.WorkDayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Calendar;

@RestController
@RequestMapping("/funds")
@Slf4j
public class FundsController {
    @Autowired
    private StaffWagesService staffWagesService;
    @Autowired
    private WorkDayService workDayService;
    @Autowired
    private PunishmentService punishmentService;

    /**
     * 员工工资分页查询，按照本月工资大小降序排序
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    @Cacheable(value = "salary", key = "'salary_'+#page+'_'+#size+'_'+#name+'_'+#salary")
    public R page(int page,int size,Long id,String name,Double salary){
        return staffWagesService.pageAll(page,size,id,name,salary);
    }

    @GetMapping("/detail")
    @Cacheable(value = "salary",key = "'salasy_detail_'+#id")
    public R detail(Long id){
        LambdaQueryWrapper<WorkDay> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,WorkDay::getStaffId,id);
        WorkDay one = workDayService.getOne(queryWrapper);
        if (one==null){
            throw new RenLiException("查无次信息");
        }
        return new R(1,one);
    }


    /**
     * 修改迟到次数那些的
     */
    @PutMapping
    public R update(@RequestBody @Validated UpdateDayDto updateDayDto, HttpSession session){
        String root = (String) session.getAttribute("root");
        if (root==null){
            throw new RenLiException("请先登陆");
        }
       return new R(1,workDayService.updateAll(updateDayDto,root));
    }

    /**
     * 分页查询赏罚历史
     */
    @GetMapping("/history")
    @Cacheable(value = "salary",key = "'history_'+#page+'_'+#size+'_'+#description")
    public R history(String description,int page,int size){
      return  punishmentService.pageDsscription(description,page,size);
    }


    /**
     * 每月一号初始化薪资表
     * @return
     */
    @PostMapping
    public R updateMonthOne(){
    return staffWagesService.updateMonthOne();
    }

}
